package com.vallis.asfc.controller;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vallis.asfc.entity.EventOnMatch;
import com.vallis.asfc.entity.MatchEvent;
import com.vallis.asfc.service.EventOnMatchService;
import com.vallis.asfc.service.MatchEventService;
import com.vallis.asfc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.net.http.HttpRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-08-11
 */
@RestController
@CrossOrigin
@RequestMapping("/match-event")
public class MatchEventController {
    
    @Autowired
    private MatchEventService matchEventService;
    @Autowired
    private EventOnMatchService eventOnMatchService;
    
    @GetMapping("getMatchEvent")
    public Result getMatchEvent(Long id) {
        return null;
        
    }
    
    @GetMapping("getEvents")
    public Result getEvents(@RequestHeader String Authorization) {
        
        
        System.out.println("获取所有项目");
        
        return new Result(200, "成功", matchEventService.list());
    }
    
    @GetMapping("getThisMatchEvents")
    public Result getThisMatchEvents(Long matchId) {
        System.out.println("开始获取这个比赛可以添加的项目" + matchId);
        List<EventOnMatch> existEvents = eventOnMatchService.list(new QueryWrapper<EventOnMatch>().eq("matchid", matchId));
        System.out.println("这个比赛已经存在的项目数" + existEvents.size());
        List<MatchEvent> allEvents = matchEventService.list();
        System.out.println("总项目数" + allEvents.size());
        List<Long> existEventsIds = new ArrayList<>();
        List<Long> allEventsIds = new ArrayList<>();
        existEvents.forEach(item -> {
            existEventsIds.add(item.getEventid());
        });
        allEvents.forEach(item -> {
            
            allEventsIds.add(item.getId());
        });
        Collection<Long> disjunction = CollUtil.disjunction(allEventsIds, existEventsIds);
        List<MatchEvent> canAddEvent = new ArrayList<>();
        disjunction.forEach(item -> {
            
            canAddEvent.add(matchEventService.getById(item));
        });
        System.out.println("还能添加的项目数" + canAddEvent.size());
        
        return new Result(200, "成功", canAddEvent);
    }
    
    @PutMapping("addEvent")
    public Result addEvent(@RequestBody MatchEvent matchEvent) {
        System.out.println(matchEvent);
        return new Result(200, "成功", matchEventService.save(matchEvent));
    }
    
    @PutMapping("removeEvent")
    public Result removeEvent(Long id) {
        
        return new Result(200, "成功", matchEventService.removeById(id));

    }
}
